class Node:
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next


class List:
    def __init__(self):
        self.head = Node()
        self.length = 0

    def insertmy(self,pos,data):
        assert pos >= 0
        if self.length > 0:
            assert pos <= self.length

        # find out the position to insert
        tmpPtr = self.head
        while pos > 0:
            tmpPtr = tmpPtr.next
            pos -= 1

        # insert new node
        newNode = Node()
        newNode.data = data
        newNode.next = tmpPtr.next
        tmpPtr.next = newNode
        self.length += 1

    def appendmy(self,data):
        self.insertmy(self.length, data)

    def removemy(self,pos):
        assert pos < self.length

        tmpPtr = self.head
        while pos > 0:
            tmpPtr = tmpPtr.next
            pos -= 1

        if tmpPtr.next is not None:
            delNode = tmpPtr.next
            tmpPtr.next = delNode.next
            self.length -= 1

    def foreach(self):
        tmpPtr = self.head.next
        while tmpPtr is not None:
            yield tmpPtr.data
            tmpPtr = tmpPtr.next

    def __iter__(self):
        return self.foreach()

if __name__ == "__main__":
    l = List()
    l.appendmy(1)
    l.appendmy(2)
    l.appendmy(3)
    l.appendmy(4)
    for v in l:
        print(v)

    l.removemy(1)
    for v in l:
        print(v)



